
public class Main {
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    public void create(){
        ListNode node1 = new ListNode(0);
        ListNode node2 = new ListNode(1);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(3);
        ListNode node5 = new ListNode(4);
        //做连接
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = null;//可以不加
        this.head = node1;
    }
    public ListNode reverseList(ListNode head){
        ListNode cur=head;
        ListNode prev=null;
        while(cur!=null){
            ListNode tpm=cur.next;
            cur.next=prev;
            prev=cur;
            cur=tpm;
        }
        return prev;
    }
    public void printList(ListNode head){
        while(head!=null){
            System.out.print(head.val + " ");
            head=head.next;
        }
        System.out.println();
    }
    public static void main(String[] args) {
        Main m=new Main();
        m.create();
        System.out.println("翻转前:");
        m.printList(m.head);
        System.out.println("翻转后:");
        ListNode head=m.reverseList(m.head);
        m.printList(head);

    }
}
